package com.demo.modules.mq.rabbit.service.header;

import com.demo.modules.mq.rabbit.RabbitAnnouncementMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 消息生产者
 *
 * @author Rog
 */
@Slf4j
@Component
public class RabbitHeaderMessageProducerService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送到header交换机
     */
    public void headerQueue() {
        String exeChange = "wr.header";
        MessagePostProcessor postProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //指定消息头
                message.getMessageProperties().setHeader("id", "121");
                return message;
            }
        };
        //发送的消息
        RabbitAnnouncementMessage rabbitAnnouncementMessage = new RabbitAnnouncementMessage();
        rabbitAnnouncementMessage.setId("1");
        rabbitAnnouncementMessage.setContent("helle,tow queue");
        //指定key，对应交换器的过滤字符
        rabbitTemplate.convertAndSend(exeChange, "", rabbitAnnouncementMessage, postProcessor);
    }


}

